import {Page} from '@/base/Page';
import {MainTain} from '@/base/model/MainTain';
import {isEmpty} from '@/base/util/Strings';
import {
  delWorker,
  getDealerList,
  getMaintainList,
  getWorkerList,
  postDealer,
  postWorker,
  delPostsale,
  createWorker
} from '@/base/api/ServiceApi';
import {formatDate} from '@/base/util/Dates';
import {getAgentList} from '@/base/api/AgentApi';
import {deepAssign} from '@/base/util/Objects';
import {Agent} from '@/base/model/Agent';
import {getDeviceModel} from '@/base/api/DeviceModelApi';
import {DeviceModel} from '@/base/model/DeviceModel';

class Form {
  createBegin = '';
  createEnd = '';
  deviceSn = '';
  name = '';
  phone = '';
  date = [];
  state = '';
  modelName='';
  agentId=0;
  provinceId=0;
  province = '';
  cityId=0;
  city='';
  stateList = [
    {
      value: '0',
      label: '已取消'
    },
    {
      value: '1',
      label: '待分配'
    },
    {
      value: '2',
      label: '已分配待接单'
    },
    {
      value: '5',
      label: '已接单待预约'
    },
    {
      value: '6',
      label: '已预约待上门'
    },
    {
      value: '7',
      label: '已上门待完成'
    },
    {
      value: '4',
      label: '已维修'
    }
  ]
}
class Worker {
  name = '';
  phone = '';
  pw = '';
  cpw = '';
}
export class MaintainBloc {
  form = new Form();
  worker = new Worker();
  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  paged: Page<MainTain> = new Page<MainTain>([], 0, 1, 15, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);
  agentList: any = [];
  modelList: any = [];
  loading = false;
  allosymbol = false;
  dealersymbol = false;
  addsymbol = false;
  selectedWorker = 0;
  selectedDealer = 0;
  WorkerArray = [];
  DealerArray = [];

  transymbol = false;
  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = formatDate(new Date(this.form.date[0].getTime()), 'yyyy-MM-dd') || '';
      // @ts-ignore
      this.form.createEnd = formatDate(new Date(this.form.date[1].getTime() + 24 * 60 * 60 * 1000), 'yyyy-MM-dd') || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getMaintainList({
      pageIndex,
      pageSize,
      type: 0,
      deviceSn: this.form.deviceSn,
      name: this.form.name,
      tel: this.form.phone,
      sortDirection: 'ASC',
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
      state: this.form.state,
      searchAgentId: this.form.agentId,
      cityId: this.form.cityId,
      modelName: this.form.modelName,
      provinceId: this.form.provinceId
    }).then((res) => {
      this.paged = res.data;
      // console.log(this.paged);
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllAngent(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    return getAgentList({
      pageIndex,
      pageSize,
      companyId: undefined
    }).then((res) => {
      deepAssign(this.angent, res.data);
      this.agentList = this.angent.items.map(function (item) {
        let obj = { id: 0, agentName: '' };
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.agentList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllModel(pageIndex: number, pageSize: number, sortId, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId: undefined,
      id: undefined,
      modelName: ''
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj;
      });
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);

  }

  allocation(dealerid) {
    return getWorkerList({
      dealerId: dealerid
    }).then((res) => {
      // eslint-disable-next-line no-eval
      let data = eval('(' + res.data + ')');
      if (data.errCode === 200) {
        this.allosymbol = true;
        this.WorkerArray = data.errMsg;
      }
    })
  }
  trans(dealerid) {
    return getDealerList({
      dealerId: dealerid
    }).then((res) => {
      // eslint-disable-next-line no-eval
      let data = eval('(' + res.data + ')');
      if (data.errCode === 200) {
        this.dealersymbol = true;
        this.DealerArray = data.errMsg;
      }
    })
  }
  workerSubmit(UserID, id) {
    return postWorker({
      id: id,
      userId: UserID
    }).then((res) => {
        return res;
    })
  }
  dealerSubmit(dealerID, id) {
    return postDealer({
      id: id,
      dealerId: dealerID
    }).then((res) => {
      return res;
    })
  }
  deldelallocation(id, dealId) {
    return delWorker({
      id: id,
      dealerId: dealId
    }).then((res) => {
      return res;
    })
  }
  delPostsale(id, dealId, sym) {
    this.loading = true;
    return delPostsale({
      id: id,
      dealerId: dealId,
      sym: sym
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  save(dealerID) {
    this.loading = true;
    return createWorker({
      dealerId: dealerID,
      nickName: this.worker.name,
      phone: this.worker.phone,
      ps: this.worker.pw
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

}
